package com.game.web;

import javax.ejb.EJB;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

import com.game.service.IMessageService;
import com.game.util.CustomTokenValue;
import com.game.util.VerifyUtils;

@Path("game")
public class GameController {
	
	@Inject
	HttpServletRequest request;
	@EJB
	IMessageService messageService;
	
	/**
	 * 接收微信服务器的验证信息
	 * @param signature
	 * @param timestamp
	 * @param nonce
	 * @param echostr
	 * @return
	 */
	@GET
	public String verify(@QueryParam("signature") String signature,
						@QueryParam("timestamp") String timestamp,
						@QueryParam("nonce") String nonce,
						@QueryParam("echostr") String echostr){
		boolean flag = VerifyUtils.isEqual(signature, timestamp, nonce, "chai");
		
		if(flag){
			return echostr;
		}
		return null;
	}
	
	
	@POST
	public String responseText(String message){
		System.out.println(message);
		//首先判断是否是微信服务器发来的消息
		//获取加密签名，随机数，时间戳
		String signature = request.getParameter("signature");
		String nonce = request.getParameter("nonce");
		String timestamp = request.getParameter("timestamp");
		if(VerifyUtils.isEqual(signature, timestamp, nonce, CustomTokenValue.token)){
			//return wxService.processMessage(message);
			return messageService.processMessage(message);
		}else{
			return "非法请求";
		}
	}
}
